#include <Arduino.h>
#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <u8g2_fonts.h>
#include <U8g2_for_Adafruit_GFX.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <Timezone.h>
#include <DNSServer.h>
#include <WiFi.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <Fonts/FreeMonoBold12pt7b.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include <Fonts/FreeMonoBold24pt7b.h>
#include <DHT.h>
#include "MyImg.h"
#include "GB2312.h"
#include "Webserver.h"
#include "SolarAndLunerHeader.h"
#include <driver/adc.h>
#include <esp_adc_cal.h>

//****************************初始化****************************
//********按键引脚********
const int modePin = 32;  //  
const int keepPin = 0;  //  
const int setPin=2;
const int ledPin = 13;
const int dqPin  =21;
const int usbCheckPin = 35;
const int chargeStatePin = 16;
const int keyInPin = 32;
const int ctrlPowerPin = 17;
const int libotBatPin = 33;



//#define D4 20
//#define D5 20

//*******天气刷新时间
const int flashtime_0 = 53000; // 1/100 s  1分钟更新一次
const int flashtime_1 = 60*1000; // 1/100 s  1分钟更新一次  
const int flashtime_2 = 160000; // 1/100 s   30分钟更新一次    非精准

const int updateDhtTime = 5*60*1000;//5min
int startDhtTime=0;

//tamato clock
enum tamato_clock_state_t { CLOCK_STOP,
       CLOCK_RUNNING,
       CLOCK_PAUSE,
       CLOCK_TIMEOVER };

tamato_clock_state_t tamatoClockState = CLOCK_STOP;

const int defaultTclockMinutes = 25;//25min
int startTclockMinutes = 0;
int tmpTclockMinutes = 0;
int tclockMinutesLast = defaultTclockMinutes;

//const int updateTclockTime = 60*1000;//5min
//int startTclockTime=0;

//IO state
int usbCheckState=0;
int chargeState=0;

//adc monitor libotbattery voltage
#define DEFAULT_VREF 1100
#define ADC_WIDTH ADC_WIDTH_12Bit
#define ADC_ATTEN ADC_ATTEN_11db
esp_adc_cal_characteristics_t *adc_charts;  

//epd lib using gxepd2 lib
GxEPD2_3C<GxEPD2_213_RH, GxEPD2_213_RH::HEIGHT> display(GxEPD2_213_RH(/*CS=5*/ 15, /*DC=*/ 27, /*RST=*/ 26, /*BUSY=*/ 25)); // DEPG0213RH 104x212, SSD1680
#define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
SPIClass hspi(HSPI);

//******U8G2字体设置******
extern const uint8_t chinese_city_gb2312[239032] U8G2_FONT_SECTION("chinese_city_gb2312");
U8G2_FOR_ADAFRUIT_GFX u8g2Fonts; //创建U8G2对象

//*****默认wifi及密码*****
String ssid = "";
String pass = "";

//********配网设置********
const char *apSSID = "Epd213配网";       //配网WiFi名称
const IPAddress apIP(192, 168, 1, 1);    //配网IP地址
WebServer webServer(80);                 //配网网页端口


//****************************NTP配置****************************
#define LOCALPORT 8000     // Local port to listen for UDP packets
#define NTP_PACKET_SIZE 48 // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming & outgoing packets

// NTP服务器参数
char ntpServerName[128] = "ntp6.aliyun.com";
int timeZone = 0;
int timezone_offset = 8; //显示为东八区

//********心知天气********
String language = "zh-Hans"; // 请求语言
String authcode_default = "未填写";  //秘钥
String authcode = "";  //秘钥

//************农历**************//
Mydate lunarDate,solarDate;


//********DHT11***********//
#define DHTTYPE DHT11
DHT dht(dqPin, DHTTYPE);
float Tem=0.0;
float Hum=0.0;



//****************************用户函数****************************
//未全部列出
//******CallHttps******
String callHttps(String url);


//*********NTP*********
time_t getNtpTime();
void sendNTPpacket(IPAddress &address);
void initNTP();

//******WebServer*******
void wifi_init();
void startWebServer();
void configWebServer();
void setupMode();
void SmartConfig();

//******定制页面********
//void display_dinosaur_time_page();
//void display_weather_page();
//void display_poetry_page();
void display_wificonfig_page();
void display_dinosaur_time_page();

